package com.example.rotateimageview.editHelp

import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver

class EditListenerHelp : ViewTreeObserver.OnGlobalLayoutListener {
    private lateinit var rootView: View

    private var listener :EditListener? =null

    interface EditListener {
        fun EditShowListener()
        fun EditHideListener()
    }

    fun registerView(rootView: View) {
        this.rootView=rootView
        rootView.viewTreeObserver.addOnGlobalLayoutListener(this)
    }

    fun setEditListener(listener: EditListener) {
        this.listener = listener
    }

    fun unRegisterListener(){
        rootView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        listener=null
    }

    override fun onGlobalLayout() {
        val rec= Rect()
        rootView.getWindowVisibleDisplayFrame(rec)
        val visibleHeight= rec.bottom - rec.top
        val rootViewHeight = rootView.height
        if (rootViewHeight - visibleHeight > 500) {
            listener?.EditShowListener()
        }else{
            listener?.EditHideListener()
        }
    }
}